home *** CD-ROM | disk | FTP | other *** search
/ Programming a Multiplayer FPS in DirectX / Programming a Multiplayer FPS in DirectX (Companion CD).iso / DirectX / dxsdk_oct2004.exe / dxsdk.exe / Samples / C++ / Direct3D / EffectEdit / EffectEdit.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2004-09-27  |  13.3 KB  |  454 lines

  1. // EffectEdit.cpp : Defines the class behaviors for the application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "EffectEdit.h"
  6.  
  7. #include "MainFrm.h"
  8. #include "EffectDoc.h"
  9. #include "UIElements.h"
  10. #include "RenderView.h"
  11.  
  12. #ifdef _DEBUG
  13. #define new DEBUG_NEW
  14. #undef THIS_FILE
  15. static char THIS_FILE[] = __FILE__;
  16. #endif
  17.  
  18. /////////////////////////////////////////////////////////////////////////////
  19. // CEffectEditCommandLineInfo
  20.  
  21. CEffectEditCommandLineInfo::CEffectEditCommandLineInfo()
  22. {
  23.     m_bUseExternalEditor = FALSE;
  24. }
  25.  
  26.  
  27. void CEffectEditCommandLineInfo::ParseParam(const TCHAR* pszParam,BOOL bFlag,BOOL bLast)
  28. {
  29.     TCHAR* pstrExtEd = TEXT("ee");
  30.     if( bFlag && CompareString( LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE, pszParam, -1, pstrExtEd, -1 ) == CSTR_EQUAL )
  31.         m_bUseExternalEditor = TRUE;
  32.     else
  33.         CCommandLineInfo::ParseParam( pszParam, bFlag, bLast );
  34. }
  35.  
  36.  
  37. /////////////////////////////////////////////////////////////////////////////
  38. // CEffectEditDocManager::DoPromptFileName - overridden to allow modification
  39. // of initial dir
  40. BOOL CEffectEditDocManager::DoPromptFileName(CString& fileName, UINT nIDSTitle,
  41.             DWORD lFlags, BOOL bOpenFileDialog, CDocTemplate* pTemplate)
  42. {
  43.     CFileDialog dlgFile(bOpenFileDialog);
  44.  
  45.     CString title;
  46.     VERIFY(title.LoadString(nIDSTitle));
  47.  
  48.     dlgFile.m_ofn.Flags |= lFlags;
  49.  
  50.     TCHAR strMedia[MAX_PATH];
  51.     BOOL bDefaultToMediaDir = AfxGetApp()->GetProfileInt( TEXT("Settings"), 
  52.         TEXT("DefaultToDXSDKMediaDir"), TRUE );
  53.     if( bDefaultToMediaDir )
  54.     {
  55.         DXUTGetDXSDKMediaPathCch( strMedia, MAX_PATH );
  56.         dlgFile.m_ofn.lpstrInitialDir = strMedia;
  57.         TCHAR strMedia2[MAX_PATH];
  58.         lstrcpy( strMedia2, strMedia );
  59.         _tcsncat( strMedia2, TEXT("EffectEdit"), MAX_PATH );
  60.         strMedia2[MAX_PATH-1] = 0;
  61.         DWORD dw;
  62.         dw = GetFileAttributes( strMedia2 );
  63.         if( dw != ((DWORD)-1) && (dw & FILE_ATTRIBUTE_DIRECTORY) != 0 )
  64.             dlgFile.m_ofn.lpstrInitialDir = strMedia2;
  65.     }
  66.  
  67.     CString strFilter;
  68.  
  69.     // Can't do the usual _AfxAppendFilterSuffix thing because that function
  70.     // is local to docmgr.cpp
  71.     strFilter += TEXT("Effect Files (*.fx)");
  72.     strFilter += (TCHAR)'\0';   // next string please
  73.     strFilter += _T("*.fx");
  74.     strFilter += (TCHAR)'\0';   // last string
  75.     dlgFile.m_ofn.nMaxCustFilter++;
  76.  
  77.     // append the "*.*" all files filter
  78.     CString allFilter;
  79.     VERIFY(allFilter.LoadString(AFX_IDS_ALLFILTER));
  80.     strFilter += allFilter;
  81.     strFilter += (TCHAR)'\0';   // next string please
  82.     strFilter += _T("*.*");
  83.     strFilter += (TCHAR)'\0';   // last string
  84.     dlgFile.m_ofn.nMaxCustFilter++;
  85.  
  86.     dlgFile.m_ofn.lpstrFilter = strFilter;
  87.     dlgFile.m_ofn.lpstrTitle = title;
  88.     dlgFile.m_ofn.lpstrFile = fileName.GetBuffer(_MAX_PATH);
  89.  
  90.     INT_PTR nResult = dlgFile.DoModal();
  91.     fileName.ReleaseBuffer();
  92.     return nResult == IDOK;
  93. };
  94.  
  95.  
  96. /////////////////////////////////////////////////////////////////////////////
  97. // CEffectEditApp
  98.  
  99. BEGIN_MESSAGE_MAP(CEffectEditApp, CWinApp)
  100.     //{{AFX_MSG_MAP(CEffectEditApp)
  101.     ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
  102.     ON_COMMAND(ID_VIEW_CHOOSEFONT, OnViewChooseFont)
  103.     ON_COMMAND(ID_VIEW_TABS, OnViewTabOptions)
  104.     ON_COMMAND(ID_FILE_DEFAULTTODXSDKMEDIAFOLDER, OnFileDefaultToDxsdkMediaFolder)
  105.     ON_UPDATE_COMMAND_UI(ID_FILE_DEFAULTTODXSDKMEDIAFOLDER, OnUpdateFileDefaultToDxsdkMediaFolder)
  106.     //}}AFX_MSG_MAP
  107.     // Standard file based document commands
  108.     ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
  109.     ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
  110. END_MESSAGE_MAP()
  111.  
  112. /////////////////////////////////////////////////////////////////////////////
  113. // CEffectEditApp construction
  114.  
  115. CEffectEditApp::CEffectEditApp()
  116. {
  117.     m_bRenderContinuously = true;
  118.     m_bAppActivated = false;
  119. }
  120.  
  121. /////////////////////////////////////////////////////////////////////////////
  122. // The one and only CEffectEditApp object
  123.  
  124. CEffectEditApp theApp;
  125.  
  126. /////////////////////////////////////////////////////////////////////////////
  127. // CEffectEditApp initialization
  128.  
  129. BOOL CEffectEditApp::InitInstance()
  130. {
  131.     // Standard initialization
  132.     // If you are not using these features and wish to reduce the size
  133.     //  of your final executable, you should remove from the following
  134.     //  the specific initialization routines you do not need.
  135.  
  136. #if(_MFC_VER < 0x0700)
  137. #ifdef _AFXDLL
  138.     Enable3dControls();         // Call this when using MFC in a shared DLL
  139. #else
  140.     Enable3dControlsStatic();   // Call this when linking to MFC statically
  141. #endif
  142. #endif
  143.     // Change the registry key under which our settings are stored.
  144.     SetRegistryKey(_T("Microsoft"));
  145.  
  146.     LoadStdProfileSettings(8);  // Load standard INI file options (including MRU)
  147.  
  148.     if( !AfxInitRichEdit() )
  149.         return FALSE;
  150.  
  151.     // Register the application's document templates.  Document templates
  152.     //  serve as the connection between documents, frame windows and views.
  153.  
  154.     m_pDocManager = new CEffectEditDocManager;
  155.     
  156.     CSingleDocTemplate* pDocTemplate;
  157.     pDocTemplate = new CSingleDocTemplate(
  158.         IDR_MAINFRAME,
  159.         RUNTIME_CLASS(CEffectDoc),
  160.         RUNTIME_CLASS(CMainFrame),       // main SDI frame window
  161.         RUNTIME_CLASS(CRenderView));
  162.     AddDocTemplate(pDocTemplate);
  163.  
  164.     // Parse command line for standard shell commands, DDE, file open
  165.     CEffectEditCommandLineInfo cmdInfo;
  166.     ParseCommandLine(cmdInfo);
  167.  
  168.     // Dispatch commands specified on the command line
  169.     if (!ProcessShellCommand(cmdInfo))
  170.         return FALSE;
  171.  
  172.     if( cmdInfo.UseExternalEditor() )
  173.         m_pMainWnd->PostMessage(WM_COMMAND, ID_EDIT_USEEXTERNALEDITOR );
  174.  
  175.     return TRUE;
  176. }
  177.  
  178. void CEffectEditApp::OnFileDefaultToDxsdkMediaFolder()
  179. {
  180.     BOOL bDefaultToMediaDir = GetProfileInt( TEXT("Settings"), TEXT("DefaultToDXSDKMediaDir"), TRUE );
  181.     bDefaultToMediaDir = !bDefaultToMediaDir;
  182.     WriteProfileInt( TEXT("Settings"), TEXT("DefaultToDXSDKMediaDir"), bDefaultToMediaDir );
  183. }
  184.  
  185. void CEffectEditApp::OnUpdateFileDefaultToDxsdkMediaFolder(CCmdUI* pCmdUI)
  186. {
  187.     BOOL bDefaultToMediaDir = GetProfileInt( TEXT("Settings"), TEXT("DefaultToDXSDKMediaDir"), TRUE );
  188.     pCmdUI->SetCheck( bDefaultToMediaDir );
  189. }
  190.  
  191. /////////////////////////////////////////////////////////////////////////////
  192. // CAboutDlg dialog used for App About
  193.  
  194. class CAboutDlg : public CDialog
  195. {
  196. public:
  197.     CAboutDlg();
  198.  
  199. // Dialog Data
  200.     //{{AFX_DATA(CAboutDlg)
  201.     enum { IDD = IDD_ABOUTBOX };
  202.     CEdit   m_edtHelp;
  203.     CString m_strVersion;
  204.     //}}AFX_DATA
  205.  
  206.     // ClassWizard generated virtual function overrides
  207.     //{{AFX_VIRTUAL(CAboutDlg)
  208.     protected:
  209.     virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  210.     //}}AFX_VIRTUAL
  211.  
  212. // Implementation
  213. protected:
  214.  
  215.     //{{AFX_MSG(CAboutDlg)
  216.     virtual BOOL OnInitDialog();
  217.     //}}AFX_MSG
  218.     DECLARE_MESSAGE_MAP()
  219. };
  220.  
  221. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  222. {
  223.     TCHAR szFile[MAX_PATH];
  224.     CString strVersion;
  225.     UINT cb;
  226.     DWORD dwHandle;
  227.     BYTE FileVersionBuffer[1024];
  228.     VS_FIXEDFILEINFO* pVersion = NULL;
  229.  
  230.     GetModuleFileName(NULL, szFile, MAX_PATH);
  231.  
  232.     cb = GetFileVersionInfoSize(szFile, &dwHandle/*ignored*/);
  233.     if (cb > 0)
  234.     {
  235.         if (cb > sizeof(FileVersionBuffer))
  236.             cb = sizeof(FileVersionBuffer);
  237.  
  238.         if (GetFileVersionInfo(szFile, 0, cb, FileVersionBuffer))
  239.         {
  240.             pVersion = NULL;
  241.             if (VerQueryValue(FileVersionBuffer, L"\\", (VOID**)&pVersion, &cb)
  242.                 && pVersion != NULL) 
  243.             {
  244.                 strVersion.Format( L"Version %d.%02d.%02d.%04d", 
  245.                     HIWORD(pVersion->dwFileVersionMS),
  246.                     LOWORD(pVersion->dwFileVersionMS), 
  247.                     HIWORD(pVersion->dwFileVersionLS), 
  248.                     LOWORD(pVersion->dwFileVersionLS));
  249.             }
  250.         }
  251.     }
  252.  
  253.     //{{AFX_DATA_INIT(CAboutDlg)
  254.     m_strVersion = strVersion;
  255.     //}}AFX_DATA_INIT
  256. }
  257.  
  258. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  259. {
  260.     CDialog::DoDataExchange(pDX);
  261.     //{{AFX_DATA_MAP(CAboutDlg)
  262.     DDX_Control(pDX, IDC_HELP_EDITBOX, m_edtHelp);
  263.     DDX_Text(pDX, IDC_VERSION, m_strVersion);
  264.     //}}AFX_DATA_MAP
  265. }
  266.  
  267. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  268.     //{{AFX_MSG_MAP(CAboutDlg)
  269.     //}}AFX_MSG_MAP
  270. END_MESSAGE_MAP()
  271.  
  272. // App command to run the dialog
  273. void CEffectEditApp::OnAppAbout()
  274. {
  275.     CAboutDlg aboutDlg;
  276.     aboutDlg.DoModal();
  277. }
  278.  
  279. /////////////////////////////////////////////////////////////////////////////
  280. // CEffectEditApp message handlers
  281.  
  282.  
  283. BOOL CEffectEditApp::OnIdle(LONG lCount) 
  284. {
  285.     if( m_bRenderContinuously )
  286.     {
  287.         CWinApp::OnIdle(lCount);
  288.         if( !m_bAppActivated )
  289.             Sleep(50);
  290.         m_pMainWnd->SendMessage(WM_COMMAND, ID_VIEW_RENDER);
  291.         return TRUE; // always request more time
  292.     }
  293.     else
  294.     {
  295.         return CWinApp::OnIdle(lCount);
  296.     }
  297. }
  298.  
  299. void CEffectEditApp::ActivateTextView()
  300. {
  301.     ((CMainFrame*)m_pMainWnd)->ActivateTextView();
  302. }
  303.  
  304. void CEffectEditApp::ActivateErrorsView()
  305. {
  306.     ((CMainFrame*)m_pMainWnd)->ActivateErrorsView();
  307. }
  308.  
  309. void CEffectEditApp::ActivateOptionsView()
  310. {
  311.     ((CMainFrame*)m_pMainWnd)->ActivateOptionsView();
  312. }
  313.  
  314. void CEffectEditApp::SelectLine(int iLine)
  315. {
  316.     ((CMainFrame*)m_pMainWnd)->SelectLine(iLine);
  317. }
  318.  
  319. void CEffectEditApp::OnViewChooseFont() 
  320. {
  321.     CFontDialog fontDialog;
  322.     DWORD dwFontSize = GetProfileInt( TEXT("Settings"), TEXT("FontSize"), 9 );
  323.     CString strFontName = GetProfileString( TEXT("Settings"), TEXT("FontName"), TEXT("Courier") );
  324.  
  325.     HDC hdc = GetDC(NULL);
  326.     fontDialog.m_lf.lfHeight = -MulDiv(dwFontSize, GetDeviceCaps(hdc, LOGPIXELSY), 72);
  327.     ReleaseDC( NULL, hdc );
  328.     fontDialog.m_lf.lfWeight = FW_NORMAL;
  329.     lstrcpy( fontDialog.m_lf.lfFaceName, strFontName );
  330.     fontDialog.m_cf.Flags |= CF_INITTOLOGFONTSTRUCT;
  331.     fontDialog.m_cf.Flags &= ~CF_EFFECTS;
  332.  
  333.     if( IDOK == fontDialog.DoModal() )
  334.     {
  335.         CFont font;
  336.         if( NULL != font.CreateFontIndirect(&fontDialog.m_lf) )
  337.         {
  338.             font.DeleteObject();
  339.             int newSize = fontDialog.GetSize() / 10;
  340.             CString strNewFontName = fontDialog.GetFaceName();
  341.             WriteProfileInt( TEXT("Settings"), TEXT("FontSize"), newSize );
  342.             WriteProfileString( TEXT("Settings"), TEXT("FontName"), strNewFontName );
  343.  
  344.             ((CMainFrame*)m_pMainWnd)->TextViewUpdateFont();
  345.         }
  346.     }
  347. }
  348.  
  349. void CEffectEditApp::OnViewTabOptions() 
  350. {
  351.     CTabOptionsDialog tabDialog;
  352.  
  353.     BOOL bKeepTabs = GetProfileInt( TEXT("Settings"), TEXT("Keep Tabs"), FALSE );
  354.     INT numSpaces = GetProfileInt( TEXT("Settings"), TEXT("Num Spaces"), 4 );
  355.     
  356.     tabDialog.m_numSpaces = numSpaces;
  357.     
  358.     if( bKeepTabs )
  359.         tabDialog.m_TabsOrSpacesRadio = 0;
  360.     else
  361.         tabDialog.m_TabsOrSpacesRadio = 1;
  362.  
  363.     if( IDOK == tabDialog.DoModal() )
  364.     {
  365.         numSpaces = tabDialog.m_numSpaces;
  366.         if( tabDialog.m_TabsOrSpacesRadio == 0 )
  367.             bKeepTabs = TRUE;
  368.         else
  369.             bKeepTabs = FALSE;
  370.  
  371.         WriteProfileInt( TEXT("Settings"), TEXT("Keep Tabs"), bKeepTabs );
  372.         WriteProfileInt( TEXT("Settings"), TEXT("Num Spaces"), numSpaces );
  373.     }
  374. }
  375.  
  376. /////////////////////////////////////////////////////////////////////////////
  377. // CTabOptionsDialog dialog
  378.  
  379.  
  380. CTabOptionsDialog::CTabOptionsDialog(CWnd* pParent /*=NULL*/)
  381.     : CDialog(CTabOptionsDialog::IDD, pParent)
  382. {
  383.     //{{AFX_DATA_INIT(CTabOptionsDialog)
  384.     m_numSpaces = 0;
  385.     m_TabsOrSpacesRadio = -1;
  386.     //}}AFX_DATA_INIT
  387. }
  388.  
  389.  
  390. void CTabOptionsDialog::DoDataExchange(CDataExchange* pDX)
  391. {
  392.     CDialog::DoDataExchange(pDX);
  393.     //{{AFX_DATA_MAP(CTabOptionsDialog)
  394.     DDX_Text(pDX, IDC_NUMSPACES, m_numSpaces);
  395.     DDV_MinMaxUInt(pDX, m_numSpaces, 1, 8);
  396.     DDX_Radio(pDX, IDC_TABS, m_TabsOrSpacesRadio);
  397.     //}}AFX_DATA_MAP
  398. }
  399.  
  400.  
  401. BEGIN_MESSAGE_MAP(CTabOptionsDialog, CDialog)
  402.     //{{AFX_MSG_MAP(CTabOptionsDialog)
  403.         // NOTE: the ClassWizard will add message map macros here
  404.     //}}AFX_MSG_MAP
  405. END_MESSAGE_MAP()
  406.  
  407. /////////////////////////////////////////////////////////////////////////////
  408. // CTabOptionsDialog message handlers
  409.  
  410. BOOL CAboutDlg::OnInitDialog() 
  411. {
  412.     CDialog::OnInitDialog();
  413.     
  414.     bool bSuccess = false;
  415.  
  416.     HMODULE hModule = NULL;
  417.     HRSRC rsrc;
  418.     HGLOBAL hgData;
  419.     LPVOID pvData;
  420.     DWORD cbData;
  421.  
  422.     rsrc = FindResource( hModule, MAKEINTRESOURCE(IDR_HELP_TXT), L"TEXT" );
  423.     if( rsrc != NULL )
  424.     {
  425.         cbData = SizeofResource( hModule, rsrc );
  426.         if( cbData > 0 )
  427.         {
  428.             hgData = LoadResource( hModule, rsrc );
  429.             if( hgData != NULL )
  430.             {
  431.                 pvData = LockResource( hgData );
  432.                 if( pvData != NULL )
  433.                 {
  434.                     TCHAR* strBuffer = (TCHAR*)pvData;
  435.                     strBuffer[cbData/sizeof(TCHAR)] = 0;
  436.  
  437.                     m_edtHelp.SetWindowText( strBuffer );
  438.                     bSuccess = true;
  439.                 }
  440.             }
  441.         }
  442.     }
  443.  
  444.     if( !bSuccess )
  445.     {
  446.         CString sz( "Error: Could not open help.txt" );
  447.         m_edtHelp.SetWindowText( sz );
  448.     }
  449.  
  450.     return TRUE;  // return TRUE unless you set the focus to a control
  451.                   // EXCEPTION: OCX Property Pages should return FALSE
  452. }
  453.  
  454.